package cn.edu.heuet.bnve.fragment;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.View;
import android.view.ViewGroup;


import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.activity.SettingActivity.AboutActivity;
import cn.edu.heuet.bnve.activity.SettingActivity.InforSettingActivity;
import cn.edu.heuet.bnve.activity.SettingActivity.NotifySettingActivity;
import cn.edu.heuet.bnve.activity.SettingActivity.TableSettingActivity;
import cn.edu.heuet.bnve.activity.SettingActivity.TodoSettingActivity;
import cn.edu.heuet.bnve.base.fragment.BaseFragment;
import cn.edu.heuet.bnve.databinding.FragmentSettingBinding;



import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

public class SettingFragment extends BaseFragment {

    private SettingViewModel mViewModel;
    private FragmentSettingBinding binding;
    private XUIGroupListView mGroupListView;

    public static SettingFragment newInstance() {
        return new SettingFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_setting;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.bind(mRootView);
        mGroupListView = binding.groupListView;
        initGroupListView();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    private void initGroupListView() {
        XUICommonListItemView informationItem = mGroupListView.createItemView(
                getResources().getDrawable(R.drawable.icon_avatar1),
                "个人信息",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        informationItem.setOrientation(XUICommonListItemView.VERTICAL);
        informationItem.setTag(1);

        XUICommonListItemView scheduleItem = mGroupListView.createItemView(
                getResources().getDrawable(R.drawable.ic_widget_statelayout),
                "日历与日程",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        scheduleItem.setOrientation(XUICommonListItemView.VERTICAL);
        scheduleItem.setTag(2);

        XUICommonListItemView tableItem = mGroupListView.createItemView(
                getResources().getDrawable(R.drawable.ic_util_view),
                "课程表",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        tableItem.setOrientation(XUICommonListItemView.VERTICAL);
        tableItem.setTag(3);

        XUICommonListItemView messageItem = mGroupListView.createItemView(
                getResources().getDrawable(R.drawable.ic_timer),
                "消息通知",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        messageItem.setOrientation(XUICommonListItemView.VERTICAL);
        messageItem.setTag(4);

        XUICommonListItemView languageItem = mGroupListView.createItemView(
                getResources().getDrawable(R.drawable.ic_setting_language),
                "语言",
                "简体中文",
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        languageItem.setTag(5);

        XUICommonListItemView aboutItem = mGroupListView.createItemView(
                getResources().getDrawable(R.drawable.ico_20px),
                "关于",
                null,
                XUICommonListItemView.HORIZONTAL,
                XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        aboutItem.setOrientation(XUICommonListItemView.VERTICAL);
        aboutItem.setTag(7);

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof XUICommonListItemView) {
                    int tag;
                    try {
                        tag = (Integer) ((XUICommonListItemView) v).getTag();
                    }
                    catch (Exception e){
                        tag =0;
                    }
                    if (tag!=0) {
                        switch (tag) {
                            case 1:
                                InforSettingActivity.show(mContext);
                                break;
                            case 2:
                                TodoSettingActivity.show(mContext);
                                break;
                            case 3:
                                TableSettingActivity.show(mContext);
                                break;
                            case 4:
                                NotifySettingActivity.show(mContext);
                                break;
                            case 7:
                                AboutActivity.show(mContext);
                            default:
                                break;
                        }
                    }
                }
            }
        };

        int size = DensityUtils.dp2px(getContext(), 25);
        XUIGroupListView.newSection(getContext())
                .setTitle("基础设置")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(informationItem, onClickListener)
                .addItemView(scheduleItem, onClickListener)
                .addItemView(tableItem, onClickListener)
                .addItemView(messageItem, onClickListener)
                .addItemView(languageItem, onClickListener)
                .addTo(mGroupListView);

        XUIGroupListView.newSection(getContext())
                .setTitle("其他")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(aboutItem, onClickListener)
                .addTo(mGroupListView);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(SettingViewModel.class);
        // TODO: Use the ViewModel
    }

}